package com.lfx.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * 延迟执行
 */
public class Main {


    public static void main(String[] args) throws Exception {
//        schedule();
        scheduleCall();
    }


    /**
     * 无返回值
     */
    public static void schedule(){
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
        scheduledThreadPoolExecutor.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("延迟执行啦啦啦啦");
            }
        },3, TimeUnit.SECONDS);
        System.out.println("延迟执行");
        scheduledThreadPoolExecutor.shutdown();
    }


    /**
     * 返回值
     * @throws Exception
     */
    public static void scheduleCall() throws Exception {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);

        Callable<String> call = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("延迟执行啦啦啦啦");
                return "success";
            }
        };
        scheduledThreadPoolExecutor.schedule(call, 3, TimeUnit.SECONDS);
        System.out.println("延迟执行");
        System.out.println("延迟执行"+call.call());

        scheduledThreadPoolExecutor.shutdown();
    }
}
